package com.yijinsuo.activity;

import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.lzy.okhttputils.model.HttpParams;
import com.yijinsuo.R;
import com.yijinsuo.base.BaseActivity;
import com.yijinsuo.http.HttpCallBack;
import com.yijinsuo.http.HttpUtils;
import com.yijinsuo.mian.Config;
import com.yijinsuo.tool.SharedPreferencesUtils;
import com.yijinsuo.tool.Tool;
import com.yijinsuo.view.ShareSdkPopupWindow;
import com.yijinsuo.view.TitleBar;

import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * yihuangxing 邀请 好友
 */
public class CenterFriendsActivity extends BaseActivity {

    @Bind(R.id.titlebar)
    TitleBar titlebar;
    //
    @Bind(R.id.rel)
    RelativeLayout rel;


    //
    @Bind(R.id.pb_loading)
    ProgressBar pbLoading;
    @Bind(R.id.mWebView)
    WebView vWVContent;

    String share_url="";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_center_friends);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initTitle() {

        titlebar.setTitleBar(R.drawable.back_selector, "", 0);
        titlebar.setTitleBarBackgroundColor(getResources().getColor(R.color.tou_ming));
        titlebar.setTitleBarRightText("分享");
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void setData() {

        //初始化WebView
        vWVContent.setWebViewClient(new WebViewClient());
        vWVContent.getSettings().setJavaScriptEnabled(true);
        vWVContent.getSettings().setBuiltInZoomControls(true); //显示放大缩小 controler
        vWVContent.getSettings().setSupportZoom(true); //可以缩放
        //屏幕自适应
        vWVContent.getSettings().setUseWideViewPort(true);
        vWVContent.getSettings().setLoadWithOverviewMode(true);
        vWVContent.setWebChromeClient(new WebChromeClient() {

            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                pbLoading.setProgress(newProgress);
                if (newProgress == 100) {
                    pbLoading.setVisibility(View.GONE);
                }

            }

        });

        getFriendData();

    }

    /**
     * 调用邀请好有
     */
    private void getFriendData() {
        HttpParams params = new HttpParams();
        String token = (String) SharedPreferencesUtils.getInstance().get(this, "token", "");
        params.put("token", token);
        HttpUtils.post(Config.EXTENSION, params, new HttpCallBack(this, "加载中...") {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {

                if (null != result) {
                    String url = result.optString("url");
                    vWVContent.loadUrl(url);
                }

                //获取分享连接
                gettoShare();
            }

            @Override
            public void onError(String code, String message) {

                Toast.makeText(CenterFriendsActivity.this, message, Toast.LENGTH_SHORT).show();
            }
        });


    }

    public void gettoShare() {

        HttpUtils.get(Config.TOSHARE, new HttpCallBack(this, "加载中...") {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {

                if (null != result) {
                    share_url = result.optString("url");
                    share_url = share_url.replace("\\u003d", "=");
                }
            }

            @Override
            public void onError(String code, String message) {

            }
        });

    }

    @OnClick({R.id.titlebar_iv_back, R.id.titlebar_right_text})
    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {

            case R.id.titlebar_iv_back:

                finish();
                break;


            case R.id.titlebar_right_text:

//                if(!share_url.equals("")){
                    ShareSdkPopupWindow window = new ShareSdkPopupWindow(CenterFriendsActivity.this, "亿金所欢迎你", share_url, Tool.getImage(this));
                    // 这里是位置显示方式,在屏幕的底部
                    window.showAtLocation(rel, Gravity.BOTTOM, 0, 0);

//                }else{
//                    Toast.makeText(CenterFriendsActivity.this, "分享失败", Toast.LENGTH_SHORT).show();
//                }


                break;


            default:
        }

    }


}
